/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Client.domain;

import Client.gui.Drawable;
import java.awt.Color;
import java.awt.image.BufferedImage;

/**
 *
 * @author Ripper
 */
public abstract class ClientEntity implements Drawable
{
    private ClientGameSession session;
    
    private double x;
    private double y;
    private boolean movingNorth;
    private boolean movingSouth;
    private boolean movingEast;
    private boolean movingWest;
    private int hpprct;
    
    private int looktype;
    private Color[] coloring;
    
    //TODO: add animations and stuff, for when people walk etc
    private Animation idleAnim;
    private Animation movingRight;
    private Animation movingLeft;
    private Animation movingDown;
    private Animation movingUp;
    
    private Animation idleAnim_Selecting;
    private Animation movingRight_Selecting;
    private Animation movingLeft_Selecting;
    private Animation movingDown_Selecting;
    private Animation movingUp_Selecting;
    
    //temp:
    //private BufferedImage image;
    //private BufferedImage imageWhenSelecting;
    
    public ClientEntity(ClientGameSession gameSession, double xx, double yy, int hp, int looktyp, Color[] colors, Color selectingColor)
    {
        x = xx;
        y = yy;
        hpprct = hp;
        looktype = looktyp;
        coloring = colors;
        
        session = gameSession;
        
        //TODO: set the animations etc
        //set idle Animation
        idleAnim = new Animation();
        int idx = 1;
        try
        {
            while(true)
            {
                BufferedImage bi = session.getCdc().getImage(looktype+"_idle"+idx, coloring);
                idleAnim.addFrame(new Frame(bi, 200));
                idx++;
            }
        }catch(Exception ex) { }
        
        //set north animation
        movingUp = new Animation();
        idx = 1;
        try
        {
            while(true)
            {
                BufferedImage bi = session.getCdc().getImage(looktype+"_wn"+idx, coloring);
                movingUp.addFrame(new Frame(bi, 200));
                idx++;
            }
        }catch(Exception ex) { }
        
        //set south animation
        movingDown = new Animation();
        idx = 1;
        try
        {
            while(true)
            {
                BufferedImage bi = session.getCdc().getImage(looktype+"_ws"+idx, coloring);
                movingDown.addFrame(new Frame(bi, 200));
                idx++;
            }
        }catch(Exception ex) { }
        
        //set east animation
        movingRight = new Animation();
        idx = 1;
        try
        {
            while(true)
            {
                BufferedImage bi = session.getCdc().getImage(looktype+"_we"+idx, coloring);
                movingRight.addFrame(new Frame(bi, 200));
                idx++;
            }
        }catch(Exception ex) { }
        
        //set west animation
        movingLeft = new Animation();
        for(Frame f: movingRight.getFrames())
        {
            BufferedImage switched = f.getImage();
            
            switched = session.getCdc().mirrorHorizontally(switched);
            
            movingLeft.addFrame(new Frame(switched, 200));
        }
        
        //TODO: recreate everything for when "selecting"
        idleAnim_Selecting = new Animation();
        movingRight_Selecting = new Animation();
        movingLeft_Selecting = new Animation();
        movingUp_Selecting = new Animation();
        movingDown_Selecting = new Animation();
        for(Frame f: idleAnim.getFrames())
        {
            idleAnim_Selecting.addFrame(
                    new Frame(session.getCdc().surroundImageWithBorder(f.getImage(), selectingColor), f.getTimeDrawn())
                    );
        }
        for(Frame f: movingRight.getFrames())
        {
            movingRight_Selecting.addFrame(
                    new Frame(session.getCdc().surroundImageWithBorder(f.getImage(), selectingColor), f.getTimeDrawn())
                    );
        }
        for(Frame f: movingLeft.getFrames())
        {
            movingLeft_Selecting.addFrame(
                    new Frame(session.getCdc().surroundImageWithBorder(f.getImage(), selectingColor), f.getTimeDrawn())
                    );
        }
        for(Frame f: movingUp.getFrames())
        {
            movingUp_Selecting.addFrame(
                    new Frame(session.getCdc().surroundImageWithBorder(f.getImage(), selectingColor), f.getTimeDrawn())
                    );
        }
        for(Frame f: movingDown.getFrames())
        {
            movingDown_Selecting.addFrame(
                    new Frame(session.getCdc().surroundImageWithBorder(f.getImage(), selectingColor), f.getTimeDrawn())
                    );
        }
        
        //image = session.getCdc().getImage(looktype+"", coloring);
        //imageWhenSelecting = session.getCdc().surroundImageWithBorder(image, selectingColor);
    }
    
    //TODO: work with animations and stuff, to draw the correct image
    @Override
    public BufferedImage getImageToDraw()
    {
        if(isMovingEast())
        {
            return movingRight.getImage();
        }
        if(isMovingNorth())
        {
            return movingUp.getImage();
        }
        if(isMovingSouth())
        {
            return movingDown.getImage();
        }
        if(isMovingWest())
        {
            return movingLeft.getImage();
        }
        return idleAnim.getImage();
    }
    
    @Override
    public double getRotation()
    {
        return 0;
    }
    
    @Override
    public BufferedImage getImageToDrawWhenSelecting()
    {
        if(isMovingEast())
        {
            return movingRight_Selecting.getImage();
        }
        if(isMovingNorth())
        {
            return movingUp_Selecting.getImage();
        }
        if(isMovingSouth())
        {
            return movingDown_Selecting.getImage();
        }
        if(isMovingWest())
        {
            return movingLeft_Selecting.getImage();
        }
        return idleAnim_Selecting.getImage();
    }

    public Color[] getColoring() {
        return coloring;
    }

    public void setColoring(Color[] coloring) {
        this.coloring = coloring;
    }

    @Override
    public int getHpprct() {
        return hpprct;
    }

    public void setHpprct(int hpprct) {
        this.hpprct = hpprct;
    }

    public int getLooktype() {
        return looktype;
    }

    public void setLooktype(int looktype) {
        this.looktype = looktype;
    }

    public boolean isMovingEast() {
        return movingEast;
    }

    public void setMovingEast(boolean movingEast) {
        if(!isMovingEast() && movingEast)
        {
            movingRight.startAnimation();
            movingRight_Selecting.startAnimation();
        }
        this.movingEast = movingEast;
    }

    public boolean isMovingNorth() {
        return movingNorth;
    }

    public void setMovingNorth(boolean movingNorth) {
        if(!isMovingNorth() && movingNorth)
        {
            movingUp.startAnimation();
            movingUp_Selecting.startAnimation();
        }
        this.movingNorth = movingNorth;
    }

    public boolean isMovingSouth() {
        return movingSouth;
    }

    public void setMovingSouth(boolean movingSouth) {
        if(!isMovingSouth() && movingSouth)
        {
            movingDown.startAnimation();
            movingDown_Selecting.startAnimation();
        }
        this.movingSouth = movingSouth;
    }

    public boolean isMovingWest() {
        return movingWest;
    }

    public void setMovingWest(boolean movingWest) {
        if(!isMovingWest() && movingWest)
        {
            movingLeft.startAnimation();
            movingLeft_Selecting.startAnimation();
        }
        this.movingWest = movingWest;
    }

    @Override
    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    @Override
    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

    public ClientGameSession getSession() {
        return session;
    }

    public void setSession(ClientGameSession session) {
        this.session = session;
    }
    
    
    
    
    
}
